package com.czkevin.cif;

import de.tr7zw.nbtapi.NBTCompound;
import de.tr7zw.nbtapi.NBTCompoundList;
import de.tr7zw.nbtapi.NBTTileEntity;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerInteractEvent;

public abstract class BaseBlock extends BaseItem {

    @Override
    public final boolean isBlock() {
        return true;
    }

    @Override
    public boolean onPlace(CIFItemInstance instance, PlayerInteractEvent event) {
        Location location = event.getClickedBlock().getLocation();
        location.getWorld().playSound(location, Sound.BLOCK_STONE_PLACE, SoundCategory.AMBIENT, 1.0f, 1.0f);
        return true;
    }

    @Override
    public void onBroken(CIFItemInstance instance, Block block, Entity by) {
        if(by != null && HumanEntity.class.isAssignableFrom(by.getClass()) && ((HumanEntity) by).getGameMode() == GameMode.CREATIVE) return;
        Location location = block.getLocation();
        World world = location.getWorld();
        world.dropItem(location, CustomItemFramework.makeCIFItem(new CIFItemInstance(this, instance == null ? null : instance.data)));
    }

    public void build(Player player, Block block, CIFItemInstance instance) {
        block.setType(Material.SPAWNER);
        NBTTileEntity tile = new NBTTileEntity(block.getState());
        tile.setShort("SpawnRange", (short) 0);
        tile.setShort("SpawnCount", (short) 0);
        tile.setShort("RequiredPlayerRange", (short) 0);
        tile.setShort("MaxNearbyEntities", (short) 0);
        {
            if(tile.hasKey("SpawnData")) tile.removeKey("SpawnData");
            //NBTCompound tileSpawnData = tile.addCompound("SpawnData");
            NBTCompound tileSpawnData = tile.addCompound("SpawnData").addCompound("entity"); // 1.18 (1.17 +?)
            tileSpawnData.setString("id", "minecraft:armor_stand");
            tileSpawnData.setByte("Marker", (byte) 1);
            tileSpawnData.setInteger("Invisible", 1);
            NBTCompoundList armors = tileSpawnData.getCompoundList("ArmorItems");
            armors.addCompound();
            armors.addCompound();
            armors.addCompound();
            armors.addCompound(instance.type.generateItemNBT(instance == null || instance.type == null ? new CIFItemInstance(this) : instance, true));

            CustomItemFramework.putCIFData(tileSpawnData.addCompound(CustomItemFramework.NBT_TAG_ENTRY), this, instance == null ? null : instance.data);
        }
    }

    public boolean saveData(Player player, Block block, NBTCompound data) {
        if(block == null || block.getType() != Material.SPAWNER) return false;
        CIFItemInstance instance = CustomItemFramework.getCIFDataFromBlock(block);
        if(instance == null || instance.type != this) return false;
        NBTTileEntity tile = new NBTTileEntity(block.getState());
        NBTCompound compound = tile.getCompound("SpawnData");
        if(compound == null) return false;
        NBTCompound cif = compound.getCompound(CustomItemFramework.NBT_TAG_ENTRY);
        if(cif == null) return false;
        CustomItemFramework.putCIFData(cif, this, data);
        return true;
    }

}

